<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jquery实现轮播图</title>
    <style>
        ul {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .banner {
            width: 1200px;
            height: 500px;
            background-color: pink;
            position: relative;
            margin: 0 auto;
        }

        .imgs {
            width: 100%;
            height: 100%;
            background-color: antiquewhite;
        }

        .imgs li {
            width: 1200px;
            height: 500px;
            position: absolute;
            left: 0;
            top: 0;
            font-size: 50px;
            text-align: center;
            line-height: 500px;
            color: #fff;
            transition: all 1s ease-in;
            opacity: 0.1;
        }

        .imgs li:nth-child(1) {
            background-color: aqua;
            z-index: 1;
        }

        .imgs li:nth-child(2) {
            background-color: blue;
        }

        .imgs li:nth-child(3) {
            background-color: chartreuse;
        }

        .imgs li:nth-child(4) {
            background-color: darkcyan;
        }

        li.active {
            opacity: 1;
        }

        .btns {
            width: 200px;
            height: 30px;
            position: absolute;
            left: 500px;
            bottom: 20px;
            z-index: 999;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .btns li {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: #ccc;
        }

        li.btn_active {
            background-color: #fff;
        }

        .prev,
        .next {
            width: 60px;
            height: 80px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            top: 210px;
            z-index: 999;
        }

        .prev {
            left: 0;
        }

        .next {
            right: 0;
        }
    </style>
</head>
<body>
    <div class="banner">
        <ul class="imgs">
            <li class="active">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
        <ul class="btns">
            <li class="btn_active"></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
        <div class="prev"></div>
        <div class="next"></div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"
        integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g=="
        crossorigin="anonymous" referrerpolicy="no-referrer">
    </script>
    <script>
        let z=10;
        $(".btns li").click(function(){
            $(".btn_active").removeClass("btn_active").siblings().addBack()
            .filter(this).addClass("btn_active");
            let index=$(this).index();
            $(".active").removeClass("active");
            $(".imgs li").eq(index).addClass("active").css({zIndex:z++});
            now=index;
        })
        let now=0;
        let change=function(){
            now++;
            if (now === -1) {
                now = 3;
            }
            now %= 4;
            $(".btn_active").removeClass("btn_active");
            $(".btns li").eq(now).addClass("btn_active");
            $(".active").removeClass("active");
            $(".imgs li").eq(now).addClass("active").css({zIndex:z++});
        }
        let t=setInterval(change,3000);
        $(".banner").mouseenter(()=>{clearInterval(t)});
        $(".banner").mouseleave(()=>{t=setInterval(change,3000)});
        $(".prev").click(()=>{
            now -= 2;
            change();
        })
        $(".next").click(()=>{change();});
    </script>
</body>
</html>
